import CustomTree from './packages/custom-tree'
import LbTable from './packages/lb-table'
import TableFilter from './packages/table-filter'
import TableOperate from './packages/table-operate'
import RuleConfig from './packages/rule-config'
import TreeTransfer from './packages/tree-transfer'
import CustomForm from './packages/custom-form'
import CustomForm2 from './packages/custom-form2'
import ComplexForm from './packages/complex-form'
import CommonDialog from './packages/common-dialog'
import CommonPage from './views/Core/common-page/index'
import EEmpty from './packages/empty'
import CustomTab from './packages/custom-tab'
import FieldRender from '@admin/packages/table-filter/src/fieldRender.vue'
import ImgViewer from '@admin/components/imgViewer/index'
import EleDistypeExtend from '@admin/packages/table-filter/src/components/extend/main.vue'
import '@admin/assets/css/index.scss'
import {
  createComponents,
  createRemoteComponents
} from '@admin/packages/table-filter/src/components/extend/utils'
import {
  createTableColumn,
  createTableData
} from '@admin/views/Core/Templates/utils/table.js'
import componentMixin from '@admin/packages/table-filter/src/mixins/mixin.js'
const loadView = (view) => {
  return (resolve) => require([`@admin/views/${view}`], resolve)
}

import store from './store'
import './utils/directives.js'
import './utils/const'
import './utils/formhelper'
import {
  $timeFormate,
  $$confirm,
  $$message,
  $$success
} from './utils/prototype'
import {
  addClass
} from './utils/dom'

const components = [
  CustomTree,
  LbTable,
  TableFilter,
  TableOperate,
  RuleConfig,
  TreeTransfer,
  CustomForm,
  CustomForm2,
  ComplexForm,
  CommonDialog,
  CommonPage,
  EEmpty,
  CustomTab,
  ImgViewer,
  EleDistypeExtend
]
import complexForm2 from '@admin/packages/custom-form/src/complex'
// 显示类型组件
import eleDisTypes from './packages/table-filter/src/components/index'
// 自定义指令
import directives from './directives'
import plugins from './plugins' // plugins
console.log(directives)

const install = function (Vue, opts = {}) {
  window.qiBao = {
    $store: opts.store,
    $request: opts.request,
    $router: opts.router || {},
    $businessType: opts.BusinessType || {},
    $utils: opts.utils || {},
    $layout: opts.layout,
  }

  Object.keys(directives).forEach(direName => {
    Vue.directive(direName, directives[direName])
  })
  Vue.component('FieldRender', FieldRender)
  Object.keys(eleDisTypes).forEach(eleDisTypeName => {
    Vue.component(eleDisTypes[eleDisTypeName].name, eleDisTypes[eleDisTypeName])
  })
  components.forEach(component => {
    Vue.component(component.name, component)
  })
  try {
    if (opts.BusinessType) {
      Object.keys(opts.BusinessType).forEach(componentName => {
        Vue.component(componentName, opts.BusinessType[componentName].component)
      })
    }
  } catch (e) {
    console.error('加载客户端组件失败', e)
  }

  Vue.component('complexForm2', complexForm2)
  Vue.use(plugins)

  const eventBus = new Vue()
  Vue.prototype.$bus = eventBus
  Vue.prototype.$timeFormate = $timeFormate
  Vue.prototype.$$confirm = $$confirm
  Vue.prototype.$$message = $$message
  Vue.prototype.$$success = $$success
  Vue.prototype.$download = opts.download
  Vue.prototype.$getDownload = opts.getDownload
}

// 兼容小屏幕
const screenWidth = window.screen.width
window.screenType = 'medium'
if (screenWidth > 1280 && screenWidth < 1920) {
  // 缩放125%
  window.screenType = 'small'
  addClass(document.documentElement, 'screen-small')
} else if (screenWidth <= 1280) {
  // 缩放150%
  window.screenType = 'mini'
  addClass(document.documentElement, 'screen-mini')
}

if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}
export default {
  install,
  store,
  LbTable,
  AdminTable: loadView('system/develop/table/index'),
  AdminTableForm: loadView('system/develop/table/form/index'),
  AdminCommonPage: loadView('Core/common-page/index'),
  AdminTabPage: loadView('Core/tab-page/index'),
  tableFunc: {
    createTableColumn,
    createTableData
  },
  componentMixin,
  eleDisTypes: {
    EleDistype90: eleDisTypes.EleDistype90
  },
  utils: {
    createComponents,
    createRemoteComponents
  }
}
